<!DOCTYPE html>
<html>

<meta charset="utf-8">
<head th:include="include :: header"></head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

	<div class="row">
		<div class="col-sm-12">

			<div class="ibox-content">
				<form class="form-horizontal m-t" id="signupForm">
					<input id="userId" name="userDO.userId" th:value="${user.userId}"
						   type="hidden">
					<div class="form-group">
						<label class="col-sm-3 control-label">输入密码：</label>
						<div class="col-sm-8">
							<input id="password" name="pwdNew" class="form-control"
								   type="password" value="111111">
						</div>
					</div>
					<div class="form-group">
						<div class="col-sm-8 col-sm-offset-3">
							<button type="submit" class="btn btn-primary">提交</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>



<!-- 全局js -->
<script  th:src="@{/js/jquery.min.js?v=2.1.4}"></script>
<script  th:src="@{/js/bootstrap.min.js?v=3.3.6}"></script>

<!-- 自定义js -->
<script  th:src="@{/js/content.js?v=1.0.0}"></script>

<!-- jQuery Validation plugin javascript-->
<script  th:src="@{/js/plugins/validate/jquery.validate.min.js}"></script>
<script  th:src="@{/js/plugins/validate/messages_zh.min.js}"></script>
<script th:inline="javascript"> var ctx = [[@{/}]]; </script>
<script src="/js/crypto-js.min.js"  th:src="@{/js/crypto-js.min.js}"></script>
<script type="text/javascript">
	var type = '[[${type}]]';
	$().ready(function() {
		validateRule(type);
	});
	$.validator.setDefaults({
		submitHandler : function() {
			update();
		}
	});
	function update() {
		getKey();
		var pwd = encrypt($("#password").val());
		validateRule(type);
		$.ajax({
			cache : true,
			type : "POST",
			url : ctx + "/sys/user/adminResetPwd",
			data : {
				'userDO.userId' : $("#userId").val(),
				password : pwd
			},// 你的formid
			async : false,
			error : function(request) {
				parent.layer.msg("系统错误，联系管理员");
			},
			success : function(data) {
				if (data.code == 0) {
					parent.layer.msg(data.msg);
					parent.reLoad();
					var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
					parent.layer.close(index);

				} else {
					parent.layer.msg(data.msg);
				}

			}
		});

	}

	function getKey(){
		$.ajax({
			type: "GET",
			url: ctx+"/wxs/getKey",
			async: false,
			success: function (r) {
				secret = r;
			}
		});
	}

	/**
	 * 加密（需要先加载lib/aes/aes.min.js文件）
	 * @param word
	 * @returns {*}
	 */
	function encrypt(word){
		var key = CryptoJS.enc.Utf8.parse(secret);
		var srcs = CryptoJS.enc.Utf8.parse(word);
		var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
		return encrypted.toString();
	}

	function validateRule(type) {
		// alert(type);
		var icon = "<i class='fa fa-times-circle'></i> ";
		if (type == 1) {
			$("#signupForm").validate({
				rules : {
					pwdNew : {
						required : true,
						checkAdminPWD:true
					},
				},
				messages : {
					pwdNew : {
						required : icon + "请输入您的密码",
					},
				}
			})

			//用户名必须需包含数字和大小写字母中至少两种
			jQuery.validator.addMethod("checkAdminPWD", function(value, element) {

				var userblank = /^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[a-zA-Z0-9\W]{16,20}$/;
				return this.optional(element) ||(userblank.test(value));
			}, "需包含数字和大小写字母中至少两种字符的16-20位字符");

		}else {
			$("#signupForm").validate({
				rules : {
					pwdNew : {
						required : true,
						checkPWD:true
					},
				},
				messages : {
					pwdNew : {
						required : icon + "请输入您的密码",
					}
				},
			})

			jQuery.validator.addMethod("checkPWD", function(value, element) {

				var userblank = /^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[a-zA-Z0-9\W]{12,20}$/;
				return this.optional(element) ||(userblank.test(value));
			}, "密码必须12-20位字符 必须包含字母大小写 数字 标点符号");
		}

	}
</script>
</body>

</html>
